#include "stdafx.h"
#include "wf_blockscreate.h"

#include "pf_creation.h"
#include "pf_insert.h"
#include "pf_modification.h"

/**
 * Constructor.
 */
WF_BlocksCreate::WF_BlocksCreate(PF_Container& container,
        PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Blocks Create",
                           container, graphicView) {

    referencePoint = PF_Vector(false);
}



WF_BlocksCreate::~WF_BlocksCreate() {}



QAction* WF_BlocksCreate::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// (tr("Create Block"),
    QAction* action = new QAction(tr("&Create Block"), NULL); 
    //action->zetStatusTip(tr("Create Block"));
	action->setIcon(QIcon(":/extui/menublock.png"));
    return action;
}



void WF_BlocksCreate::init(int status) {
    PF_PreviewActionInterface::init(status);

}



void WF_BlocksCreate::trigger() {
    //deletePreview();

    //PF_Modification m(*container, graphicView);
    //m.paste(data.insertionPoint);
    //std::cout << *PF_Clipboard::instance();

    if (graphic!=NULL) {
        PF_BlockList* blockList = graphic->getBlockList();
        if (blockList!=NULL) {
            PF_BlockAttribute d =
                PF_DIALOGFACTORY->requestNewBlockDialog(blockList);

            if (!d.name.isEmpty()) {
                PF_Creation creation(container, graphicView);
                creation.createBlock(d, referencePoint, true);

                PF_InsertAttribute id(
                    d.name,
                    referencePoint,
                    PF_Vector(1.0,1.0),
                    0.0,
                    1, 1, PF_Vector(0.0,0.0)
                );
                creation.createInsert(id);
            }
        }
    }

	graphicView->redraw(LX_Define::RedrawDrawing); 

    finish();
    graphicView->killSelectActions();
}


void WF_BlocksCreate::mouseMoveEvent(PF_MouseEvent* e) {
    snapPoint(e);

    switch (getStatus()) {
    case SetReferencePoint:
        //data.insertionPoint = snapPoint(e);

        /*if (block!=NULL) {
            deletePreview();
            //preview->addAllFrom(*block);
            //preview->move(data.insertionPoint);
        	PF_Creation creation(preview, NULL, false);
        	creation.createInsert(data);
            drawPreview();
    }*/
        break;

    default:
        break;
    }
}



void WF_BlocksCreate::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        init(getStatus()-1);
    }
}



void WF_BlocksCreate::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    switch (getStatus()) {
    case SetReferencePoint:
        referencePoint = e->getCoordinate();
        trigger();
        break;

    default:
        break;

    }
}



void WF_BlocksCreate::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetReferencePoint:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify reference point"),
                                            tr("Cancel"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_BlocksCreate::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_BlocksCreate::updateToolBar() {
    if (!isFinished()) {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
    } else {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarMain);
    }
}


// EOF
